package com.huilian.iotbox.admin.producer;

import com.alibaba.fastjson.JSONObject;
import com.huilian.iotbox.admin.config.DelayMQConfig;
import com.huilian.iotbox.data.po.TicketRecord;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.UUID;

/**
 * RabbitProducer
 *
 * @Author mineChen
 * @Date 2021/9/7 下午 03:30
 */
@Component
public class RabbitProducer {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void sendReplayLazyQueue(Map<String, String> map, Integer delay) {
        //发送消息时指定 header 延迟时间
        rabbitTemplate.convertAndSend(DelayMQConfig.IOT_BOX_ADMIN_LAZY_EXCHANGE, DelayMQConfig.BOX_ADMIN_LAZY_QUEUE_KEY, map,
                msg -> {
                    //设置消息持久化
                    msg.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
                    //message.getMessageProperties().setHeader("x-delay", "6000");
                    msg.getMessageProperties().setDelay(delay);
                    return msg;
                }, new CorrelationData(UUID.randomUUID().toString()));
    }

    public void sendReplayLazyQueueToClient(String step, Object json, Integer delay) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("step", step);
        jsonObject.put("json", json);
        //发送消息时指定 header 延迟时间
        rabbitTemplate.convertAndSend(DelayMQConfig.IOT_BOX_CLIENT_LAZY_EXCHANGE, DelayMQConfig.BOX_CLIENT_LAZY_QUEUE_KEY, jsonObject.toJSONString(),
                msg -> {
                    //设置消息持久化
                    msg.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
                    //message.getMessageProperties().setHeader("x-delay", "6000");
                    msg.getMessageProperties().setDelay(delay);
                    return msg;
                }, new CorrelationData(UUID.randomUUID().toString()));
    }

    /**
     * 延迟队列，到时间活动刷开始状态
     *
     * @param delayMap
     * @param intValue
     */
    public void sendReplayActivityStartStateLazyQueue(Map<String, String> delayMap, int intValue) {
        rabbitTemplate.convertAndSend(DelayMQConfig.IOT_BOX_WAP_LAZY_EXCHANGE, DelayMQConfig.IOT_BOX_WAP_ACTIVITY_START_STATE_LAZY_QUEUE_KEY, delayMap,
                msg -> {
                    //设置消息持久化
                    msg.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
                    //message.getMessageProperties().setHeader("x-delay", "6000");
                    msg.getMessageProperties().setDelay(intValue);
                    return msg;
                }, new CorrelationData(UUID.randomUUID().toString()));
    }

    /**
     * 延迟队列，到时间活动刷结束状态
     *
     * @param delayMap
     * @param intValue
     */
    public void sendReplayActivityEndStateLazyQueue(Map<String, String> delayMap, int intValue) {
        rabbitTemplate.convertAndSend(DelayMQConfig.IOT_BOX_WAP_LAZY_EXCHANGE, DelayMQConfig.IOT_BOX_WAP_ACTIVITY_END_STATE_LAZY_QUEUE_KEY, delayMap,
                msg -> {
                    //设置消息持久化
                    msg.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
                    //message.getMessageProperties().setHeader("x-delay", "6000");
                    msg.getMessageProperties().setDelay(intValue);
                    return msg;
                }, new CorrelationData(UUID.randomUUID().toString()));
    }

    /**
     * 延迟队列，客户端核销卡劵
     *
     * @param step
     * @param json
     * @param delay
     */
    public void sendVerifyReplayLazyQueue(String step, Object json, Integer delay) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("step", step);
        jsonObject.put("json", json);
        //发送消息时指定 header 延迟时间
        rabbitTemplate.convertAndSend(DelayMQConfig.IOT_BOX_CLIENT_LAZY_EXCHANGE, DelayMQConfig.IOT_BOX_CLIENT_VERIFY_UP_COIN_KEY, jsonObject.toJSONString(),
                msg -> {
                    //设置消息持久化
                    msg.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
                    //message.getMessageProperties().setHeader("x-delay", "6000");
                    msg.getMessageProperties().setDelay(delay);
                    return msg;
                }, new CorrelationData(UUID.randomUUID().toString()));
    }

    public void sendBeAboutToExpireTicketRecordReplayLazyQueue(TicketRecord ticketRecord, Long l) {
        rabbitTemplate.convertAndSend(DelayMQConfig.IOT_BOX_ADMIN_LAZY_EXCHANGE, DelayMQConfig.BOX_ADMIN_BEABOUTTOEXPIRE_TICKETRECORD_LAZY_QUEUE_KEY, ticketRecord,
                msg -> {
                    //设置消息持久化
                    msg.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
                    msg.getMessageProperties().setDelay(l.intValue());
                    return msg;
                }, new CorrelationData(UUID.randomUUID().toString()));
    }
}
